﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace WHW_NetUtility
{
    class HardwareInfo
    {


        public string CpuID;          //CPU序列号
        public string MacAddress;     //网卡地址（序列号）
        public string DiskID;         //硬盘序列号
        public string BaseBoard;      //主板信息
        public string SystemTypeBit;  //系统类型
        public string PhysicalMemory; //内存大小
        public string LoginName;      //登陆用户名
        public string ComputerName;   //电脑用户名

        // 构造函数
        public HardwareInfo()
        {
            CpuID = getCpuID();
            MacAddress = getMacAddress();
            DiskID = getDiskID();
            BaseBoard = getBaseBoard();
            SystemTypeBit = getSystemType();
            PhysicalMemory = getPhysicalMemory();
            LoginName = getLoginName();
            ComputerName = getComputerName();
        }

        //获取CPU序列号
        string getCpuID()
        {
            string CpuID_Info = null;
            try
            {
                ManagementObjectSearcher mos_cpu = new ManagementObjectSearcher("Select * From Win32_Processor");
                foreach (ManagementObject mo_cpu in mos_cpu.Get())
                {
                    CpuID_Info = mo_cpu.Properties["ProcessorId"].Value.ToString().Trim();
                }
                mos_cpu = null;
                return CpuID_Info;
            }
            catch
            {
                return "CPU信息获取错误，请检查";
            }
        }

        //获取网卡序列号
        string getMacAddress()
        {
            string MacAddress_Info = null;
            try
            {
                ManagementObjectSearcher mos_Mac = new ManagementObjectSearcher("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");
                foreach (ManagementObject mo_Mac in mos_Mac.Get())
                {

                    MacAddress_Info = mo_Mac["MacAddress"].ToString().Trim();
                }
                mos_Mac = null;
                return MacAddress_Info;
            }
            catch
            {
                return "未正确检测网卡地址";
            }
        }

        //获取硬盘序列号
        string getDiskID()
        {
            string DiskID_Info = null;
            try
            {
                ManagementObjectSearcher mos_Disk = new ManagementObjectSearcher("Select * From Win32_DiskDrive");
                foreach (ManagementObject mo_Disk in mos_Disk.Get())
                {
                    DiskID_Info = mo_Disk.Properties["SerialNumber"].Value.ToString().Trim();  //查询硬盘序列号，和用CMD查出来的ID号不一样
                }
                mos_Disk = null;
                return DiskID_Info;
            }
            catch
            {
                return "未正确检测硬盘地址";
            }
        }

        //获取主板信息
        string getBaseBoard()
        {
            string BaseBoard_Info = null;
            try
            {
                ManagementObjectSearcher mos_Board = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
                foreach (ManagementObject mo_Board in mos_Board.Get())
                {
                    BaseBoard_Info = mo_Board.Properties["SerialNumber"].Value.ToString().Trim();  //查询硬盘序列号，和用CMD查出来的ID号不一样
                }
                mos_Board = null;
                return BaseBoard_Info;
            }
            catch
            {
                return "未正确检测主板信息";
            }
        }

        //获取系统类型 64位还是32位
        string getSystemType()
        {
            string SystemType_Info = null;
            try
            {
                ManagementObjectSearcher mos_System = new ManagementObjectSearcher("Select * From Win32_ComputerSystem");
                foreach (ManagementObject mo_System in mos_System.Get())
                {
                    SystemType_Info = mo_System["SystemType"].ToString().Trim();  //查询硬盘序列号，和用CMD查出来的ID号不一样
                }
                mos_System = null;
                return SystemType_Info;
            }
            catch
            {
                return "未正确检测系统信息";
            }
        }

        //获取内存大小 单位为字节 要换算成GB 需要返回值除以1024（KB）*1024（MB）*1024（GB）
        string getPhysicalMemory()
        {
            string PhysicalMemory_Info = null;
            try
            {
                ManagementObjectSearcher mos_Memory = new ManagementObjectSearcher("Select * From Win32_ComputerSystem");
                foreach (ManagementObject mo_Memory in mos_Memory.Get())
                {
                    PhysicalMemory_Info = mo_Memory["TotalPhysicalMemory"].ToString().Trim();  //查询硬盘序列号，和用CMD查出来的ID号不一样
                }
                mos_Memory = null;
                return PhysicalMemory_Info;
            }
            catch
            {
                return "未正确检测系统信息";
            }
        }

        //获取登陆用户名
        string getLoginName()
        {
            try
            {
                return Environment.UserName;
            }
            catch
            {
                return "未正确检测登陆名";
            }
        }

        //获取计算机名
        string getComputerName()
        {
            try
            {
                return Environment.MachineName;
            }
            catch
            {
                return "未正确检测计算机名";
            }
        }


        /// <summary>
        /// 获取指定硬盘的总容量
        /// </summary>
        /// <param name="str_HardDiskName">硬盘名称，大写字母，如C、D、E等</param>
        /// <returns>返回查询到的总容量</returns>
        public static long GetHardDiskSpace(string str_HardDiskName)
        {
            long totalSize = new long();//指定硬盘盘符的总容量
            //long freeSize = new long();//指定硬盘盘符的可用容量
            str_HardDiskName = str_HardDiskName +":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();//检索电脑上的硬盘名称
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    totalSize = drive.TotalSize / (1024 * 1024 * 1024);
                }
            }
            return totalSize;
        }

        /// <summary>
        /// 获取指定硬盘的剩余容量
        /// </summary>
        /// <param name="str_HardDiskName">硬盘名称，大写字母，如C、D、E等</param>
        /// <returns>返回查询到的剩余容量</returns>
        public static long GetHardDiskFreeSpace(string str_HardDiskName)
        {
            long freeSpace = new long();
            str_HardDiskName = str_HardDiskName + ":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)

            {
                if (drive.Name == str_HardDiskName)
                {
                    freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
                }
            }
            return freeSpace;
        }
    }
}
